package logic

import (
	"context"
	"taobao-merchandise/models/mysql"
	"taobao-merchandise/taobaoRpc/internal/svc"
	"taobao-merchandise/taobaoRpc/taobaoRpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateGoodLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateGoodLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateGoodLogic {
	return &UpdateGoodLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateGoodLogic) UpdateGood(in *taobaoRpc.UpdateGoodRequest) (*taobaoRpc.UpdateGoodResponse, error) {
	// todo: add your logic here and delete this line

	good := mysql.Good{}
	mysql.DB.Where("userid=? and id=?", in.UserId, in.Id).Find(&good)

	good.Title = in.Title
	good.Image = in.Image
	good.Price = float64(in.Price)

	mysql.DB.Updates(&good)

	return &taobaoRpc.UpdateGoodResponse{
		Id: int64(good.ID),
	}, nil
}
